<template>
  <!--引用表格-->
  <BasicTable @register="registerTable" :rowSelection="rowSelection">
    <!--插槽:table标题-->
    <template #tableTitle>
      <a-button type="primary" preIcon="ant-design:plus-outlined" @click="handleCreate"> 新增</a-button>
      <a-button type="primary" preIcon="ant-design:edit-outlined" @click="handleOpenGroup">字典组</a-button>

      <ExcelButton :config="excelConfig" />
      <a-button type="primary" @click="handlerRefreshCache" preIcon="ant-design:sync-outlined"> 刷新缓存</a-button>
      <a-button type="primary" @click="openRecycleModal(true)" preIcon="ant-design:hdd-outlined"> 回收站</a-button>

      <a-dropdown v-if="selectedRowKeys.length > 0">
        <template #overlay>
          <a-menu>
            <a-menu-item key="1" @click="batchHandleDelete">
              <Icon icon="i-ant-design:delete-outlined" />
              删除
            </a-menu-item>
          </a-menu>
        </template>
        <a-button
          >批量操作
          <Icon icon="i-ant-design:down-outlined" />
        </a-button>
      </a-dropdown>
    </template>
    <!--操作栏-->
    <template #action="{ record }">
      <TableAction :actions="getTableAction(record)" />
    </template>
  </BasicTable>
  <DictModal @register="registerModal" @success="handleSuccess" />
  <DictItemList @register="registerDrawer" />
  <DictRecycleBinModal @register="registerModal1" @success="reload" />
  <MyDataGroupList @register="registerGroupDrawer" @success="reload" :bizType="DataGroupBizType.DICT" />
</template>

<script lang="ts" name="system-dict" setup>
  //ts语法
  import { ref, computed, unref } from "vue"
  import { BasicTable, TableAction } from "/@/components/Table"
  import { useDrawer } from "/@/components/Drawer"
  import { useModal } from "/@/components/Modal"
  import DictItemList from "./components/DictItemList.vue"
  import DictModal from "./components/DictModal.vue"
  import DictRecycleBinModal from "./components/DictRecycleBinModal.vue"
  import { useMessage } from "/@/hooks/web/useMessage"
  import * as cache from "/@/utils/cache/module"
  import { columns, searchFormSchema } from "./dict.data"
  import { useListPage } from "/@/hooks/system/useListPage"
  import {
    list,
    deleteDict,
    batchDeleteDict,
    getExportUrl,
    getImportUrl,
    refreshCache,
    queryAllDictItems,
    deleteGroup,
    batchDeleteGroup,
    saveOrUpdateGroup,
  } from "/@/api/infra/dict"
  import { useRoute, useRouter } from "vue-router"
  import { MyDataGroupList, DataGroupBizType } from "/@/components/BizDataGroup/index"
  import { Icon } from "/@/components/Icon"
  import ExcelButton from "/@/components/My/ExcelButton.vue"

  const router = useRouter()
  let dictCode = router.currentRoute.value.query?.dictCode
  console.log("currentRoute.params: " + router.currentRoute.value.query)

  const { createMessage } = useMessage()
  //字典model
  const [registerModal, { openModal }] = useModal()
  const [registerDrawer, { openDrawer }] = useDrawer()
  const [registerGroupDrawer, { openDrawer: openGroupDrawer }] = useDrawer()
  const [registerModal1, { openModal: openRecycleModal }] = useModal()

  const excelConfig = ref({
    exportConfig: {
      name: "字典",
      url: () => getExportUrl,
      params: {},
    },
    importConfig: {
      url: () => getImportUrl,
      name: "字典模板",
      showModal: true,
      success: (file) => reload(),
      tipArticleCode: "import_dict",
    },
  })

  const { prefixCls, tableContext, onExportXls, onImportXls } = useListPage({
    designScope: "dict-template",
    tableProps: {
      title: "数据字典",
      api: list,
      beforeFetch: (params) => {
        if (dictCode) {
          params.dictCode = dictCode
        }
        excelConfig.value.exportConfig.params = params
      },
      bordered: false,
      columns: columns,
      formConfig: {
        labelWidth: 90,
        schemas: searchFormSchema,
      },
      actionColumn: {
        width: 150,
        title: "操作",
        dataIndex: "action",
        fixed: "right",
        slots: { customRender: "action" },
      },
    },
    exportConfig: {
      name: "数据字典列表",
      url: getExportUrl,
    },
    importConfig: {
      url: getImportUrl,
    },
  })

  //注册table数据
  const [registerTable, { reload, updateTableDataRecord }, { rowSelection, selectedRowKeys }] = tableContext

  function handleOpenGroup() {
    openGroupDrawer(true, {
      title: "字典分组",
      width: 800,
      deleteApi: deleteGroup,
      batchDeleteApi: batchDeleteGroup,
      saveOrUpdateApi: saveOrUpdateGroup,
    })
  }

  /**
   * 新增事件
   */
  function handleCreate() {
    openModal(true, {
      isUpdate: false,
    })
  }
  /**
   * 编辑事件
   */
  async function handleEdit(record: Recordable) {
    openModal(true, {
      record,
      isUpdate: true,
    })
  }
  /**
   * 详情
   */
  async function handleDetail(record) {
    openModal(true, {
      record,
      isUpdate: true,
    })
  }
  /**
   * 删除事件
   */
  async function handleDelete(record) {
    await deleteDict({ id: record.id }, reload)
  }
  /**
   * 批量删除事件
   */
  async function batchHandleDelete() {
    await batchDeleteDict({ ids: selectedRowKeys.value }, reload)
  }
  /**
   * 成功回调
   */
  function handleSuccess({ isUpdate, values }) {
    if (isUpdate) {
      updateTableDataRecord(values.id, values)
    }
    reload()
  }
  /**
   * 刷新缓存
   */
  async function handlerRefreshCache() {
    const res = await queryAllDictItems()
    cache.removeDictData()
    cache.setDictItems(res.data)
    createMessage.success("刷新缓存完成！")
  }
  /**
   * 字典配置
   */
  function handleItem(record) {
    openDrawer(true, { id: record.id, typeCode: record.dictCode })
  }
  /**
   * 操作栏
   */
  function getTableAction(record) {
    return [
      {
        label: "编辑",
        disabled: !record.hasPerm,
        onClick: handleEdit.bind(null, record),
      },
      {
        label: "配置",
        onClick: handleItem.bind(null, record),
      },
      {
        label: "删除",
        color: "error",
        disabled: !record.hasPerm,
        popConfirm: {
          title: "确定删除吗?",
          confirm: handleDelete.bind(null, record),
        },
      },
    ]
  }
</script>

<style scoped></style>
